package com.liao.ten;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.liao.ten.article.ArticleFragment;
import com.liao.ten.image.ImageFragment;
import com.liao.ten.moive.MovieFragment;
import com.liao.ten.user.UserFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    RadioGroup mainRg;
    Fragment movieFrag, articleFrag, imageFrag, userFrag;
    private FragmentManager fm;
    List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg = findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);
        fragments = new ArrayList<>();
        fm = getSupportFragmentManager();
        addFragmentPage();
    }

    /* 添加Fragment页面到主页面当中*/
    private void addFragmentPage() {
        FragmentTransaction transaction = fm.beginTransaction();
        movieFrag = new MovieFragment();
        transaction.add(R.id.main_layout, movieFrag);
        transaction.commit();
        fragments.add(movieFrag);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
//        FragmentTransaction bt = fm.beginTransaction();
        Fragment fragment = null;
        switch (checkedId) {
            case R.id.main_rb_moive:
                if (movieFrag == null) {
                    movieFrag = new MovieFragment();
//                    fragments.add(movieFrag);
//                    bt.add(R.id.main_layout,movieFrag);
                }
                fragment = movieFrag;
                break;
            case R.id.main_rb_article:
                if (articleFrag == null) {
                    articleFrag = new ArticleFragment();
//                    fragments.add(articleFrag);
//                    bt.add(R.id.main_layout,movieFrag);
                }
                fragment = articleFrag;
                break;
            case R.id.main_rb_image:
                if (imageFrag == null) {
                    imageFrag = new ImageFragment();
//                    fragments.add(imageFrag);
//                    bt.add(R.id.main_layout,movieFrag);
                }
                fragment = imageFrag;
                break;
            case R.id.main_rb_me:
                if (userFrag == null) {
                    userFrag = new UserFragment();
//                    fragments.add(userFrag);
//                    bt.add(R.id.main_layout,movieFrag);
                }
                fragment = userFrag;
                break;
        }
//        bt.commit();
        mangerFragment(fragment);
    }

    private void mangerFragment(Fragment showFragment) {
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            transaction.hide(fragments.get(i));
        }
        if (!fragments.contains(showFragment)) {
            transaction.add(R.id.main_layout, showFragment);
            fragments.add(showFragment);
        }
        transaction.show(showFragment);
        transaction.commit();
    }

}
